package de.derhess.projects.pkr.component {
	import de.derhess.projects.pkr.models.FactorSum;
	import de.derhess.projects.pkr.models.PKRBaseData;
	
	import spark.components.SkinnableContainer;
	import spark.layouts.VerticalLayout;
	
	/**
	 * Your class description
	 * 
	 *
	 * @author Florian Weil [derhess.de, Deutschland]
	 * @see http://blog.derhess.de
	 * 
	 */
	
	public class BaseDataContainer extends SkinnableContainer
	{
		//--------------------------------------------------------------------------
		//
		//  Class variables
		//
		//--------------------------------------------------------------------------
		private static const DEFAULT_HEIGHT:Number = 200;
		private static const DEFAULT_WIDTH:Number = 200;
		
		private static var BOUNDARY_VALUES:Array = new Array(300,1500,5000,10000,25000,50000, 200000, 500000, 99999999);
		private static var STEPS:Array = new Array(300, 300, 500,1000,3000,5000,15000, 30000, 50000, 99999999);
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		private var rvgBaseValues:Array;
		private var rvgIncreases:Array;
		
		private var gkgBaseValues:Array;
		private var gkgIncreases:Array;
		
		//--------------------------------------------------------------------------
		//
		//  Initialization
		//
		//--------------------------------------------------------------------------
		public function BaseDataContainer()
		{
			rvgBaseValues = new Array(25,25,105,301,486,686,1046,1816,2996);
			rvgIncreases = new Array(0,20,28,37,40,72,77,118,150);
			
			gkgBaseValues = new Array(25,25,65,121,196,311,456,1456,2956);
			gkgIncreases = new Array(0,10,8,15,23,29,100,150,150);
			
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
		}
		
		
		
		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		private var _dataModel:PKRBaseData;
		private var dataModelChanged:Boolean = false;
		/**
		 * This is dataModel
		 */
		public function get dataModel():PKRBaseData
		{
			return _dataModel;
		}
		
		/*
		* @private
		*/
		public function set dataModel(val:PKRBaseData):void
		{
			if(_dataModel != val) {
				_dataModel = val;
				dataModelChanged = true;
				invalidateProperties();
			}
		}
		
		
		private var _numberClients:int = 0;
		private var numberClientsChanged:Boolean = false;
		/**
		 * This is numberClients
		 */
		public function get numberClients():int
		{
			return _numberClients;
		}
		
		/*
		* @private
		*/
		public function set numberClients(value:int):void
		{
			if(_numberClients != value) {
				_numberClients = value;
				numberClientsChanged = true;
				invalidateProperties();
			}
		}
		
		private var _numberEnemies:int = 0;
		private var numberEnemiesChanged:Boolean = false;
		/**
		 * This is numberEnemies
		 */
		public function get numberEnemies():int
		{
			return _numberEnemies;
		}
		
		/*
		* @private
		*/
		public function set numberEnemies(value:int):void
		{
			if(_numberEnemies != value) {
				_numberEnemies = value;
				numberEnemiesChanged = true;
				invalidateProperties();
			}
		}
		
		private var _valueInDispute:Number = 0;
		private var valueInDisputeChanged:Boolean = false;
		/**
		 * This is valueInDispute
		 */
		public function get valueInDispute():Number
		{
			return _valueInDispute;
		}
		
		/*
		* @private
		*/
		public function set valueInDispute(value:Number):void
		{
			if(_valueInDispute != value) 
			{
				_valueInDispute = value;
				valueInDisputeChanged = true;
				this.invalidateProperties();
			}
		}
		
		private var rateVATChanged:Boolean = false;
		private var _rateVAT:Number;
		/**
		 * This is rateVAT
		 */
		public function get rateVAT():Number
		{
			return _rateVAT;
		}
		
		/*
		* @private
		*/
		public function set rateVAT(value:Number):void
		{
			if(_rateVAT != value) {
				rateVATChanged = true;
				_rateVAT = value;
				invalidateProperties();
			}
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  Additional getters and setters
		//
		//--------------------------------------------------------------------------
		public function setValueInDispute(value:Number):void
		{
			
		}

		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		override protected function measure():void
		{
			super.measure();
			measuredMinHeight = measuredHeight = DEFAULT_HEIGHT;
			measuredMinWidth = measuredWidth = DEFAULT_WIDTH;
		}
		
		
		override protected function commitProperties():void 
		{
			super.commitProperties();
			
			if(valueInDisputeChanged && this.dataModel)
			{
				this.dataModel.valueInDispute = valueInDispute;
				this.dataModel.lawyerFee = calcRVGfee(valueInDispute);
				this.dataModel.courtFee = calcGKGfee(valueInDispute);
				valueInDisputeChanged = false;
			}
			
			
			if(dataModelChanged) {
				this.dataModel.amountClients = numberClients;
				this.dataModel.amountEnemyClients = numberEnemies;
				this.dataModel.valueInDispute = valueInDispute;
				this.dataModel.lawyerFee = calcRVGfee(valueInDispute);
				this.dataModel.courtFee = calcGKGfee(valueInDispute);
				this.dataModel.rateVAT = rateVAT;
				dataModelChanged = false;
			}
			
			if(rateVATChanged && this.dataModel) {
				this.dataModel.rateVAT = rateVAT;
				rateVATChanged = false;
			}
			
			if(numberClientsChanged  && this.dataModel) {
				this.dataModel.amountClients = numberClients;
				numberClientsChanged = false;
			}
			
			if(numberEnemiesChanged && this.dataModel) {
				this.dataModel.amountEnemyClients = numberEnemies;
				numberEnemiesChanged = false;
			}
		}
		
		override protected function updateDisplayList(unscaledWidth:Number,unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
		}
		
		override protected function partAdded(partName:String,instance:Object):void
		{
			
		}
		
		override protected function partRemoved(partName:String,instance:Object):void
		{
			
		}
		

		
		//----------------------------------
		//  Berechnung Gebühr nach RVG §13 --> Rechtsanwalt Vergütungsgebühr
		//----------------------------------
		private function calcRVGfee(disputeValue:Number):Number 
		{
			var i:int= 0;
			var stepCounter:int = 0;
			
			if (disputeValue>0){
				while( (BOUNDARY_VALUES[i] < disputeValue)  &&  (i <= BOUNDARY_VALUES.length))
				{
					i++;
				}
				stepCounter = int( ( (disputeValue - BOUNDARY_VALUES[Math.max(i-1,0)]) -0.001 ) / STEPS[i] ) + 1;
				return (stepCounter * rvgIncreases[i] + rvgBaseValues[i]);
			} else {
				return 0;
			}
		}
		
		//----------------------------------
		//  Berechnung Gebühr nach GKG §34 --> Gerichtskostengebühr
		//----------------------------------
		private function calcGKGfee (disputeValue:Number):Number
		{
			var i:int = 0;
			var stepCounter:int = 0;
			
			if (disputeValue > 0){
				while( (BOUNDARY_VALUES[i]<disputeValue) && (i <= BOUNDARY_VALUES.length)){
					i++;
				}
				stepCounter = int( ( (disputeValue - BOUNDARY_VALUES[Math.max(i-1,0)]) - 0.001)/ STEPS[i] ) + 1;
				return (stepCounter * gkgIncreases[i] + gkgBaseValues[i]);
			} else {
				return 0;
			}	
		}
		
		//--------------------------------------------------------------------------
		//
		//  API
		//
		//--------------------------------------------------------------------------
		
		
		//--------------------------------------------------------------------------
		//
		//  Eventhandling
		//
		//--------------------------------------------------------------------------
		
		
		//--------------------------------------------------------------------------
		//
		//  Broadcasting
		//
		//--------------------------------------------------------------------------
		
		
	}
	}
	